from django.http import HttpResponse
from utils.resFormatUtil import ResFormat
def verifyParam(*param):    # email, passwrod 不定长
    """
    参数验证装饰器
    1.校验必备参数是否录入
    2.校验参数格式是否正确
    3.发送邮件
    4.响应
    :param param:需要验证的参数
    :return:
    """
    # 装饰器func--->get/post
    def verify(func):
        def isNone(request, *args, **kwargs): # request.GET/POST.get()
            # 获取参数
            request_param = getattr(request, request.method)  # getattr()获取指定对象的指定属性 request.GET/POST
            # 循环获取要校验的参数
            for i in param:  # i假设--->email
                value = request_param.get(i, None) # request.GET/POST.get('email')

                if not value:
                    # return HttpResponse("参数错误")
                    return ResFormat(status='PARAM_ERROR').json_response()

            return func(request, *args, **kwargs)  # 回调
        return isNone
    return verify
